package com.zzg.controller;

import java.text.ParseException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.zzg.annotation.PrometheusMetrics;
import com.zzg.domain.Journal;
import com.zzg.repository.JournalRepository;

@Controller
@RequestMapping("/api/h2")
public class JournalController {
	@Autowired
	private JournalRepository repository;
	
	@RequestMapping(value="/add",method={RequestMethod.GET})
    public void add(){
		try {
			Journal journal = new Journal("Get to know Spring Boot1","Today I will learn Spring Boot","01/01/2016");
			repository.save(journal);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			System.out.println(e.getMessage());
		}
    }
	
	@RequestMapping(value="/select",method={RequestMethod.GET})
	@ResponseBody
	public String slelect(){
		List<Journal> list = repository.findAll();
		return JSON.toJSONString(list);
	}

}
